package com.example.demo.module.repository.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.repository.ProductRepositoryPoJo;
import com.example.demo.entity.request.repository.ProductRepositoryRequest;
import com.example.demo.entity.response.repository.ProductRepositoryResponse;
import com.example.demo.module.repository.service.ProductRepositoryService;
import com.example.demo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 成品入库控制层
 *
 * @author Mei Ruoxiao
 * @date 2020-09-18
 */
@Slf4j
@Api(tags = "成品入库接口")
@RestController
@RequestMapping("/product/repository")
public class ProductRepositoryController {
    @Resource
    private ProductRepositoryService service;

    /**
     * 获取成品入库列表
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取成品入库列表")
    public ResponseEntity<Rs<Page<ProductRepositoryResponse>>> page(
            @ApiParam("状态") @RequestParam(value = "state", required = false) final Integer state,
            @ApiParam("开工开始日期")
            @RequestParam(value = "start", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
            @ApiParam("开工结束日期")
            @RequestParam(value = "end", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
            @ApiParam(value = "页面", example = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1")
                    int page,
            @ApiParam(value = "页面大小", example = "10")
            @RequestParam(value = "size", required = false, defaultValue = "10")
                    int size,
            @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId) {
        log.info("page:获取成品入库列表[state:{}, start:{}, end:{}, page:{}, size:{}, companyId:{}]", state, start, end, page, size, companyId);
        Page<ProductRepositoryPoJo> poJoPage = service.page(state, start, end, page, size, companyId);
        Page<ProductRepositoryResponse> responsePage = CommonUtils.toPage(poJoPage, ProductRepositoryResponse::of);
        return Rs.ok(CommonUtils.getResponse(responsePage));
    }


    /**
     * 入库
     */
    @RequestMapping("/warehouse")
    @ApiOperation(value = "入库")
    public ResponseEntity<Rs<Object>> warehouse(@RequestBody ProductRepositoryRequest request,
                                                @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) long companyId) {
        log.info("warehouse:入库[request:{}, companyId:{}]", request, companyId);
        return Rs.ok(service.warehouse(request, companyId));
    }

}
